<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Welcome extends CI_Controller {

	public function __construct()
	{
		parent::__construct();
		$this->load->helper('url');
		$this->load->database();
	}
	
	public function index()
	{
		if(!$this->session->userdata('background'))
		$this->session->set_userdata('background','orange.jpg');
		if(!$this->session->userdata('style'))
		$this->session->set_userdata('style', 'main.css');
		
		$menu_data['title'] = "Restaurant";
		$menu_data['username'] = $this->session->userdata('username');
		$menu_data['style'] = $this->session->userdata('style');
		$menu_data['background'] = $this->session->userdata('background');
		$this->load->view('menu', $menu_data);
		//$this->load->view('welcome_body');
		$this->load->view('footer');
	}
	
	function news()
	{
		$this->db->order_by('name', 'random');
		$data['query'] = $this->db->get('restaurants', 3);
		$menu_data['title'] = "Restaurant - News";
		
		$this->load->view('menu', $menu_data);
		$this->load->view('news_view',$data);
		$this->load->view('footer');
	}
	
	public function power()
	{
		$this->load->view('openid/example');
	}
	
	public function power2()
	{
		$this->load->view('openid/example-google');
	}
	

	
	function add_restaurant1()
	{
		$this->load->library('form_validation');
		$this->form_validation->set_rules('name', 'Restaurant name', 'required|min_length[2]');
		$this->form_validation->set_rules('address_street', 'Street', 'required|min_length[3]');
		$this->form_validation->set_rules('address_zipcode', 'Zipcode', 'required|min_length[5]');
		$this->form_validation->set_rules('address_city', 'City', 'required|min_length[3]');
		$this->form_validation->set_rules('opening_hours', 'Opening Hours', 'required|min_length[5]');
		$this->form_validation->set_rules('fastfood', 'Fastfood', 'required|min_length[5]');
		$this->form_validation->set_rules('vegetarian', 'Vegetarian', 'required|min_length[5]');
		$this->form_validation->set_rules('takeaway', 'Takeaway', 'required|min_length[5]');
		$this->form_validation->set_rules('alcohol', 'Alcohol', 'required|min_length[5]');
		$this->form_validation->set_rules('kitchen', 'Kitchen', 'required|min_length[5]');
		
		$data['name'] = $this->input->post('name');
		$data['author'] = $this->input->post('author');
		$data['address_street'] = $this->input->post('address_street');
		$data['address_zipcode'] = $this->input->post('address_zipcode');
		$data['address_city'] = $this->input->post('address_city');
		$data['opening_hours'] = $this->input->post('opening_hours');
		$data['fastfood'] = $this->input->post('fastfood');
		$data['vegetarian'] = $this->input->post('vegetarian');
		$data['takeaway'] = $this->input->post('takeaway');
		$data['alcohol'] = $this->input->post('alcohol');
		$data['kitchen'] = $this->input->post('kitchen');
		
		//call for the model to evaluate and insert data into the database
		$this->load->model('add_restaurants_model');
		if($this->add_restaurants_model->add_restaurant($data))
			$data['msg'] = 'Restaurant "'.$data['name'].'" have been successfully added!';
		else
			$data['msg'] = 'A restaurant with the name '.$data['name'].' do already exists.';
			
		
		//setup menu data
		$menu_data['title'] = "Restaurant - Add Retaurant";
		
		//load the views
		$this->load->view('menu', $menu_data);
		if($this->form_validation->run() == false)
			$this->load->view('add_restaurant_view', $data);
		else
			redirect(site_url('browse_restaurants_view'), 'refresh');
		$this->load->view('footer');
		//load the views /
	}
	
	public function test()
	{
		$this->load->view('test');
	}
	
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */